package com.white.commonlib.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;

import com.white.commonlib.utils.NetUtil;

/**
 * Created by ytf on 2017/3/20 020.
 * Description: 网络状态变化接收器
 */

public class NetStateChangeReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction()))
        {
            if(l != null)
            {
                //判断网络是否连接
                boolean c = NetUtil.isNetworkConnected(context);
                final String[] net = new String[1];
                net[0] = "未连接";
                if(c)
                {
                    net[0] = NetUtil.GetNetworkType(context);
                    //判断网络是否通畅
                    NetUtil.isNetworkUnobstructed(new NetUtil.ResultCallback()
                    {
                        @Override
                        public void result(boolean b)
                        {
                            l.onChanged(net[0], b);
                        }
                    });
                }else
                {
                    l.onChanged(net[0], false);
                }
            }
        }
    }

    private NetChangeListener l;
    public void setNetChangeListener(NetChangeListener li)
    {
        this.l = li;
    }

    public interface NetChangeListener
    {
        void onChanged(String netType, boolean isNetworkOk);
    }
}
